package com.example.luke.cashorderingsystem.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.luke.cashorderingsystem.Adapter.PurchaseReportAdapter;
import com.example.luke.cashorderingsystem.Common.CommonActivity;
import com.example.luke.cashorderingsystem.Dialog.DateRangePickerDialog;
import com.example.luke.cashorderingsystem.Model.Operator;
import com.example.luke.cashorderingsystem.Model.PurchaseReport;
import com.example.luke.cashorderingsystem.R;
import com.example.luke.cashorderingsystem.Util.ExcelUtils;
import com.example.luke.cashorderingsystem.Util.GlobalFunction;
import com.example.luke.cashorderingsystem.http.HttpClient;
import com.example.luke.cashorderingsystem.http.HttpResponseHandler;
import com.example.luke.cashorderingsystem.http.NetWorkConstant;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;

import javax.microedition.khronos.opengles.GL;

import butterknife.ButterKnife;
import butterknife.InjectView;
import okhttp3.Headers;
import okhttp3.Request;

/**
 * Created by luke on 21/11/2016.
 */

public class PurchaseReportActivity extends CommonActivity {

    @InjectView(R.id.contentContainer)ListView contentContainer;
    PurchaseReportAdapter purchaseReportAdapter;
 ArrayList<HashMap>hashMapArrayList;
    DateRangePickerDialog dateRangePickerDialog;
@InjectView(R.id.dateRange)TextView intervalDate;
@InjectView(R.id.noItem)LinearLayout noItem;
    @InjectView(R.id.container)RelativeLayout container;

@InjectView(R.id.total_price)TextView totalPrice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.purchase_report_layout);
        ButterKnife.inject(this);
        showBackButton();
        setBarTitle(GlobalFunction.getString(this,R.string.purchaseReport));
       initView();



    }
   public void showDatePicker(View view){
        dateRangePickerDialog.show();

    }

    @Override
    public void initView() {
        super.initView();
        purchaseReportAdapter=new PurchaseReportAdapter(this) ;

        contentContainer.setAdapter(purchaseReportAdapter);
        hashMapArrayList=new ArrayList<>();


        showRightButtonWithListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (purchaseReportAdapter.getPurchaseReportArrayList().size()>0)

                    uploadPurchaseRecord();

                else {


                    GlobalFunction.ToastAlert(PurchaseReportActivity.this,"目前没有消费记录可上传");


                }



            }

        },GlobalFunction.getString(this,R.string.upload));

            dateRangePickerDialog=new DateRangePickerDialog(this);
            dateRangePickerDialog.setDateListener(new DateRangePickerDialog.DateListener() {
                @Override
                public void getDate(String startDate, String endDate) {

                reloadData(startDate,endDate);
                }
            });
            reloadData(GlobalFunction.getCurrentDate(),GlobalFunction.getCurrentDate());

        calcTotalPrice();

    }
    private void calcTotalPrice(){
       Double cost=0.;

        for (PurchaseReport report : purchaseReportAdapter.getPurchaseReportArrayList()){
            Double saleCost=Double.valueOf(report.getCost());
            cost=cost+saleCost;

        }

        String value=String.format(Locale.getDefault(),"%.2f",cost)+"元";

        totalPrice.setText(value);

    }
    private void reloadData(String startDate,String endDate){
        try {
            String interVal=startDate+"-"+endDate;
            intervalDate.setText(interVal);

          ArrayList<PurchaseReport>purchaseReports=  PurchaseReport.getPurchaseReportOnTypeAndInterval(context,startDate,endDate);
            if (purchaseReports.size()>0){
                showContent(true);
                purchaseReportAdapter.setPurchaseReportArrayList(purchaseReports);

            }else {

                showContent(false);
            }

        }catch (Exception e){

        GlobalFunction.printTextByTag(e.getMessage());

        }


    }
    private void showContent(boolean stat){
        if (stat){

            container.setVisibility(View.VISIBLE);
            noItem.setVisibility(View.GONE);

        }else {

            container.setVisibility(View.GONE);
            noItem.setVisibility(View.VISIBLE);
        }
    }

    public void uploadPurchaseRecord(){


        ExcelUtils.exportExcel(  purchaseReportAdapter.getPurchaseReportArrayList());

    }
    private  void  uploadReport(){
        HashMap<String,Object>body=new HashMap<>();
        body.put("num",String.valueOf(hashMapArrayList.size()));
        body.put("item",hashMapArrayList);





        HttpClient.postWithFunction(NetWorkConstant.HTTP_uploadPurchaseRecord,body,new HttpResponseHandler(){
            @Override
            protected void handleSuccessMessage(int statusCode, Headers headers, String responseBody) {
                super.handleSuccessMessage(statusCode, headers, responseBody);

                GlobalFunction.printTextByTag(responseBody);

                GlobalFunction.ToastAlert(PurchaseReportActivity.this,GlobalFunction.getString(PurchaseReportActivity.this,
                        R.string.uploadSuc));

               restart();

            }

            @Override
            protected void handleFailureMessage(Request request, IOException e) {
                super.handleFailureMessage(request, e);
            }
        });
    }
}
